package sprite;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;


public class Animator extends Transform implements ActionListener{
	private HashMap<String, Animation> _frames;
	private Timer _tm;
	private int _currentFrame = 0;
	private int _maxFrame = 0;
	private Animation _currentAnim;
	public static HashMap<String, ArrayList<GameAnimations>> gameAnimations = new HashMap<String,ArrayList<GameAnimations>>();

	public Animator(String name){
		super();
		_frames = new HashMap<String, Animation>();
		_tm = new Timer(120,this);
		_tm.start();
		for(int i = 0;i<=gameAnimations.get(name).size()-1;i++){
			this.addAnimation(gameAnimations.get(name).get(i).animations, gameAnimations.get(name).get(i).animname);
		}
	}

	public void addAnimation(Animation anim, String name){
		_frames.put(name,anim);
	}
	
	public void setAnimation(String name){
		if(_frames.get(name)!=null){
			_currentFrame = 0;
			_currentAnim = _frames.get(name);
			_maxFrame = _currentAnim.getFrameCount()-1;
		}else{
			System.out.println("ERROR: Animation  Name "+name);
		}
	}

	public boolean isPlaying(String name){
		return (_currentAnim==_frames.get(name));
	}
	public void show(Graphics2D g){
		if(_currentAnim.getImages().get(_currentFrame) != null){
			Image img = _currentAnim.getImages().get(_currentFrame);
			g.drawImage(img.getImage(),getTransform(),null);
		}else{
			System.out.println("ERROR: Animation  NULL");
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		_currentFrame++;
		if(_currentFrame>_maxFrame){
			_currentFrame = 0;
		}
	}

	public int getCurrentFrame(){
		return _currentFrame;
	}

	public int getMaxFrame(){
		return _maxFrame;
	}
	
}
